﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;

namespace OnlineEducation.Controllers
{

    public class ErrorController : BaseController
    {
       
        public ErrorController()
        {

        }
        public IActionResult Index(int? statusCode)
        {
            var isAjax = Request.Headers["X-Requested-With"] == "XMLHttpRequest";

            if (statusCode == 401 || statusCode == 403)
            {
                if (isAjax)
                {
                    return Unauthorized();
                     
                }
                return View("AccessDenied");
            }
            else if (statusCode == 404)
            {
                if (isAjax)
                {
                    return NotFound();

                }
                return View("PageNotFound");
            }

            else 
            {
                if (isAjax)
                {
                    return BadRequest();

                }
                return View("BadRequest");
            }
        }


        public IActionResult AppError()
        {
            var exceptionHandlerPathFeature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
       
            return View();
        }


        public IActionResult PageNotFound()
        {
            string originalPath = "unknown";
            if (HttpContext.Items.ContainsKey("originalPath"))
            {
                originalPath = HttpContext.Items["originalPath"] as string;
            }
       
            return View();
        }
    }
}